/*
 * Agent Analyze is the other program, the counter part to propertyParse
 * and outputs agentreport.txt
 */


import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 *
 * @author Paul Pfaff, Paul42@my.wgu.edu
 */
public class agentAnalyze {

    /**
     * @param args the command line arguments
     */
    static Set<String> propertyTypes = new TreeSet<String>();
    static TreeMap<Integer, Double> propertyValuesByAgent = new TreeMap<Integer, Double>();
    public static void main(String[] args) throws FileNotFoundException, IOException {
       fileFind fileFind1 = new fileFind();
       fileFind1.init();
       /** step through by agent ID, putting property values into ANOTHER map, so if the agentID is the same,        
        *  if it's not it will iterate through to the next agent ID. double nested for loop to iterate.
        */
       property otherTempObj = new property();
       property tempObj = new property();
       double tempValue = 0;
       Integer tempKey = 0;
       Integer otherTempKey = 0;
       
       for(Integer key : fileFind1.agents.keySet())
       {
           tempValue = 0;
           otherTempObj = fileFind1.agents.get(key);
           Iterator<property> i = fileFind1.properties.iterator();
           while(i.hasNext())
           {
                    
                    tempObj = i.next();
                    tempKey = tempObj.getAgentID();
                    otherTempKey = otherTempObj.getAgentID();
                    
                    propertyTypes.add(tempObj.getType());
                        if(tempKey==otherTempKey){
                            tempValue = tempValue + tempObj.getPropertyValue();
                            propertyValuesByAgent.put(otherTempKey, tempValue);
                        }
                        
           }
       }
       
        //output the results of property Values
    
    Scanner console = new Scanner(System.in);
    System.out.print("Output file: ");
    String outputFileName = console.next();
    BufferedWriter out = null;
    out = new BufferedWriter(new FileWriter(outputFileName));

        try {   
            out.write("Sorted PropertyTypes");
            out.newLine();
            for(String prop: propertyTypes){
                out.write(prop);
                out.newLine();
            }
            out.newLine();
            out.newLine();
            
            for(Integer key : propertyValuesByAgent.keySet()){
                out.write("Agent ID: " + key +" Total Value: "+propertyValuesByAgent.get(key));
                out.newLine();
                }
        }
        catch (IOException ex) {
            
        }
        finally{
            out.flush();
            out.close(); 
        }
    }
    }
